import 'dart:math';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/res/colors.dart';


// Widget inputWidget(BuildContext context, String title, String value,
//     {bool required = true,
//     String hint = "请输入",
//     String inputHint = "请输入",
//     Color color = Colors.white,
//     CommonInputType keyboardType = CommonInputType.multiline,
//     Function(String)? callback}) {
//   return IconTitleTextWidget(
//     title,
//     isRequired: required,
//     padding: EdgeInsets.zero,
//     arrow: DrawableRes.rightArrow,
//     color: color,
//     hintText: hint,
//     text: value,
//   ).onTap(() async {
//     var str = await openInputPage(
//         context: context,
//         text: value,
//         hintText: inputHint,
//         title: title,
//         keyboardType: keyboardType);
//     callback!(str);
//   });
// }

Widget TitleContentWidget(
    {required String title,
      bool isRequired = false,
      bool showArrow = false,
      required Widget child,
      CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
      Color? titleColor,
      Widget? head,
      double? headPadding = 12,
      EdgeInsets? padding,
      double? titleSize,
      int? titleWeight,
      int? contentWeight,
      Alignment? childAlignment,
      double arrowGap = 0,
      Widget? arrow}) {
  return Row(
    crossAxisAlignment: crossAxisAlignment,
    children: [
      if (head != null) ...[
        head,
        Container(
          width: headPadding ?? 0,
        )
      ],
      Text.rich(TextSpan(
          text: isRequired ? "*" : "",
          style: TextStyle(fontSize: titleSize ?? 16, color: Colors.red),
          children: [
            TextSpan(
                text: title,
                style: TextStyle(
                    color: titleColor ?? Colours.gray_66,
                    fontSize: titleSize ?? 16,
                    height: 1.4))
          ])).setWeight(titleWeight ?? 2),
      contentWeight==-1?Row(
        children: [
          child.addToContainer(alignment: childAlignment),
          SizedBox(
            width: arrowGap,
          ),
          arrow ?? Container()
        ],
      ):
      Row(
        children: [
          child.addToContainer(alignment: childAlignment).setWeight(1),
          SizedBox(
            width: arrowGap,
          ),
          arrow ?? Container()
        ],
      ).setWeight(contentWeight ?? 3)
    ],
  ).addToContainer(padding: padding ?? EdgeInsets.symmetric(vertical: 15));
}

Widget TitleValueWidget({
  required String? title,
  required String? value,
  String hint = '请选择',
  bool isRequired = false,
  bool showArrow = false,
  Color? titleColor,
  Color? textColor,
  TextAlign? textAlign,
  Widget? head,
  double? headPadding = 12,
  EdgeInsets? padding,
  double? titleSize,
  double? textSize,
  int? titleWeight,
  Alignment? childAlignment,
  int? contentWeight,
  Color? hinColor,
  CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
  Widget? arrow,
  double arrowGap = 0,
}) {
  return TitleContentWidget(
      title: title??"",
      titleColor: titleColor,
      head: head,
      arrowGap: arrowGap,
      headPadding: headPadding,
      childAlignment: childAlignment,
      child: CommonText(value,
          textSize: textSize ?? 16,
          height: 1.4,
          textColor: textColor ?? Colors.black,
          hintText: hint,
          textAlign: textAlign ?? TextAlign.start,
          hintColor: hinColor ?? Colours.gray_99),
      showArrow: showArrow,
      padding: padding,
      isRequired: isRequired,
      titleSize: titleSize,
      titleWeight: titleWeight,
      contentWeight: contentWeight,
      crossAxisAlignment: crossAxisAlignment,
      arrow: arrow);
}

Widget NoBorderInput({
  String? text,
  String? hintText,
  String? helpText,
  String? errorText,
  TextStyle? helpTextStyle,
  TextStyle? errorTextStyle,
  InputBorder? errorBorder,
  Widget? counter,
  EdgeInsets padding = EdgeInsets.zero,
  double textSize = 16,
  Color? textColor,
  Color? hintColor,
  InputBorder border = InputBorder.none,
  final ValueChanged<String>? onChanged,
  final ValueChanged<String>? onSubmitted,
  int? maxLines,
  int? minLines,
  Widget? headWidget,
  bool needClear = false,
  Widget? clearWidget,
  bool? filled,
  bool isCollapsed = true,
  bool showPassword = false,
  bool? isDense,
  TextAlign textAlign = TextAlign.start,
  Widget? visiblePasswordWidget,
  Widget? hidePasswordWidget,
  CommonInputType keyboardType = CommonInputType.text,
  bool autoFocus = false,
  FocusNode? focusNode,
  final TextInputAction? textInputAction,
  bool inEditModel = true,
  int? maxLength,
}) {
  return inEditModel? CommonInput(
      text: text,
      hintText: hintText,
      helpText: helpText,
      helpTextStyle: helpTextStyle,
      errorText: errorText,
      errorTextStyle: errorTextStyle,
      errorBorder: errorBorder,
      textSize: textSize,
      maxLines: maxLines,
      minLines: minLines ?? 1,
      clearWidget: clearWidget,
      onSubmitted: onSubmitted,
      onChanged: onChanged,
      border: border,
      maxLength: maxLength,
      hintColor: hintColor,
      textColor: textColor,
      headWidget: headWidget,
      padding: padding,
      counter: counter,
      needClear: needClear,
      filled: filled,
      isCollapsed: isCollapsed,
      showPassword: showPassword,
      visiblePasswordWidget: visiblePasswordWidget,
      hidePasswordWidget: hidePasswordWidget,
      textAlign: textAlign,
      autoFocus: autoFocus,
      focusNode: focusNode,
      textInputAction: textInputAction,
      isDense: isDense,
      keyboardType: keyboardType):CommonText(text);
}


class FunctionGrid extends StatelessWidget {
  List<Widget> children = [];
  int count;
  num crossSpace = 0;
  num mainSpace = 0;

  List<List<Widget>> _itemList = [];

  FunctionGrid(
      {required this.children,
        required this.count,
        this.crossSpace = 0,
        this.mainSpace = 0}) {
    _itemList = chunkList(children, count);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: _itemList
          .map((e) {
        var list = <Widget>[];
        list.addAll(e);
        if (list.length < count) {
          list.add(Container());
        }
        return Row(
          children: list
              .map((widget) => widget.setWeight(1))
              .toList()
              .insertWidget(SizedBox(
            width: crossSpace.toDouble(),
          )),
        );
      })
          .toList()
          .insertWidget(SizedBox(
        height: mainSpace.toDouble(),
      )),
    );
  }

  List<List<Widget>> chunkList(List<Widget> list, int chunkSize) {
    final List<List<Widget>> chunks = [];
    for (var i = 0; i < list.length; i += chunkSize) {
      final int end = min(i + chunkSize, list.length);
      chunks.add(list.sublist(i, end));
    }
    return chunks;
  }
}